package com.example.volleyball.playcharting.android;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import sharedClasses.Action;
import sharedClasses.Play;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewSwitcher;

/**
 * @author NicoleWeigl
 * This class is an activity used to display the results of a seasonal analysis.  
 * When launched from menu the activity executes a task that fetches the seasonal play recommendations and 
 * adds them to the results array.
 * Each set of plays is drawn on the screen depending on which selection is chosen from the spinner 
 * drop down menu. 
 */
public class AnalysisActivity extends Activity{
	private Paint p;
	private float cLeft;
	private float cTop;
	private float cBottom;
	private float cRight;
	private Bitmap bitmap1;
	private Canvas c;
	Rect[] zones=new Rect[12];
	private Rect court;
	private ImageView view;
	ViewSwitcher viewSwitch;
	TextView view1;
	TextView key1;
	TextView key2;
	TextView key3;
	Spinner spin;
	String[] resultLabels={"All Rotations", "Rotation 1", "Rotation 2", "Rotation 3",
		"Rotation 4", "Rotation 5", "Rotation 6"};
	Play[][] results= new Play[7][3];
    final int[] colors = {Color.parseColor("#FF6600"),Color.MAGENTA,Color.BLUE,Color.parseColor("#336600")};
    final TextView[] keys = {key1, key2, key3};

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //lock the screen orientation to landscape mode 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.season_layout); //set layout for activity
        
        view = (ImageView)findViewById(R.id.imageView2);
        
        //set the keys to the correct TextView and establish key color 
        key1 = (TextView)findViewById(R.id.text5);
        key2 = (TextView)findViewById(R.id.text6);
        key3 = (TextView)findViewById(R.id.text7);
        key1.setTextColor(Color.parseColor("#FF6600"));
        key2.setTextColor(Color.MAGENTA);
        key3.setTextColor(Color.BLUE);
 
		SeasonAnalysisTask mess = new SeasonAnalysisTask();
		mess.execute();
		try
		{
			results = mess.get();
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
   
        drawBlankCourt();

        spin = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapt; 
        adapt = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
        adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //the only results added to the spinners are the ones that are not null 
        for(int i=0;i<7;i++){
        	if(results[i]!=null)
        		adapt.add(resultLabels[i]);
        }
        spin.setAdapter(adapt); //set the adapter for the spinner 
        spin.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int pos,long id) {
				//reset the court to blank when view is changed 
				drawBlankCourt();
				for(int i=0;i<7;i++){
				    if(parent.getItemAtPosition(pos).equals(resultLabels[i])){
					    for(int j=0;j<3;j++){
					    	if(results[i][j]!=null){
					    		//make key visible 
					    		keys[j].setVisibility(View.VISIBLE);
					    		//code to draw the actions from specific play
					    		for(int k=0;k<results[i][j].Children.size();k++){
								    Action act = results[i][j].Children.get(k);
								    //convert coordinates 
								    int[] origin = BEUIConverter.convertBEtoUI(act.getOrigin());
								    int[] destination = BEUIConverter.convertBEtoUI(act.getDestination());
								    p.setStyle(Style.FILL);
								    //draw start point 
								    if(k==0){
								      p.setColor(Color.GREEN);
								      c.drawCircle(cLeft+(origin[0])*20, cTop+(origin[1])*20,8,p);
								    }
								    //draw end point 
								    if(k==results[i][j].Children.size()-1){
								      p.setColor(Color.RED);
								      c.drawCircle(cLeft+(destination[0])*20, cTop+(destination[1])*20,8,p);
								    }
								    p.setStyle(Style.STROKE);
						    		p.setColor(colors[j]);
						    		//draw line 
								    c.drawLine(cLeft+(0+origin[0])*20, cTop+(0+origin[1])*20, 20*(destination[0]+0)+cLeft, 20*(0+destination[1])+cTop, p);
								    System.err.println("(" + origin[0] + ", " + origin[1] + ") --> (" + destination[0] + ", " + destination[1] + ")");
								    view.invalidate();
								  }
					    	}
					    	else{
					    		 //remove the key portion for the null return  
					    		keys[j].setVisibility(View.GONE);
					    	}
					    }
				    }
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub			
			}
        });
        
	}
	
	//draw the volleyball court without any plays 
	private void drawBlankCourt(){
	    Display currentDisplay = getWindowManager().getDefaultDisplay();
    	p = new Paint();
    	p.setColor(Color.BLACK);
        p.setStyle(Style.STROKE);
	    p.setStrokeWidth(4);
	    
	    //get screen coordinates so that the court will be scaled relative to each device 
	    float dw = currentDisplay.getWidth();
        float dh = currentDisplay.getHeight();
        int scale = 20;
	         
        cLeft=(dw-(scale*60))/2;
        cRight=(dw-cLeft);
        cTop=10;
        cBottom=cTop+(scale*30);

	    int zoneH=(int) ((cBottom-cTop)/3);
	    int zoneW=(int) ((cRight-cLeft)/4);
        int count =0;
	          
	    bitmap1 = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_8888);
	    c= new Canvas(bitmap1);
	    c.drawLine(cLeft+(cRight-cLeft)/2, cTop, cLeft+(cRight-cLeft)/2, cBottom, p);
	    p.setColor(Color.GRAY);
	    p.setStrokeWidth(2);
	    for(int i=0;i<4;i++){
	      	for(int j=0;j<3;j++){
	          	zones[count]=new Rect();
	          	zones[count].set((int)cLeft+(i*zoneW),(int)cTop+(j+1)*zoneH,(int)cLeft+(i+1)*zoneW,(int)cTop+(j*zoneH));
	          	c.drawRect(zones[count], p);
	          	count++;
	         }
	    }
	    p.setColor(Color.BLACK);
	    p.setStyle(Style.STROKE);
	    p.setStrokeWidth(4);	         
	    court= new Rect();
	    court.set((int)cLeft,(int)cBottom,(int)cRight,(int)cTop);
	    c.drawRect(court, p);
	    view.setImageBitmap(bitmap1);
	}
}
